showcase.admin = liwe.module ( "showcase.admin", null, "Showcase" );

showcase.admin._tags_list = [];
showcase.admin._dest_div = 'system-content';
showcase.admin._cur_id_conf = 0;
showcase.admin._cur_conf_name = '';


//{{{ FUNZIONI DISMESSE
/*
showcase.admin._get_tags_list = function ()
{
	if ( showcase.admin._tags_list.length > 0 ) return;
	showcase.admin.ajax ( { action : "tags.ajax.tags_list_all", module : "showcase" }, function ( data ) 
						{
							if ( data [ 'tags' ] )
								data [ 'tags' ].iterate ( function ( v )
									{
										showcase.admin._tags_list.push ( { label : v.name, value : v.name } );
									} );
						} );
};





showcase.admin.list = function ( dest_div, module )
{
	showcase.admin.admin_man.show_list ( dest_div );
};


showcase.admin._filter_by = function ( dest_div )
{
	var opts = [];
	var f = new liwe.form.instance ( 'SC-filter-by' );
	f.select ( { name : 'tag', label : 'Moduli', options: showcase.admin._tags_list , force_select: true, nonl: true } );
	f.button ( { name: "cerca", value: "cerca" , onclick: "showcase.admin._filter_apply ();" } );
	f.set ( dest_div );	
};


showcase.admin._render = function ()
{
	$( showcase.admin._dest_div  , showcase.templates [ 'SC-layout' ] );
};

showcase.admin._filter_apply = function ()
{
	showcase.admin.admin_man.search_start ();
};

showcase.admin._save_item = function ()
{
	var f = liwe.form.get ( 'SC-add-form' );
	var vals = f.get_values ();
	vals [ 'action' ] = "showcase.admin.add";
	showcase.admin.ajax ( vals , function ( data ) { console.debug ( 'Salvato' ); } ); 
};
*/
//}}}


showcase.admin._set_dest_div = function ( dest_div )
{
	showcase.admin._dest_div =  dest_div;
};

showcase.admin._init_admin  = function ()
{
	var _acts = [
		/*	{ label: 'Lista elementi', func: function () { showcase.admin._render (); showcase.admin.list ( 'SC-admin-man' ); } }, 
			{ label: 'Aggiungi', func: function () { showcase.admin.add_item (); } },
		*/
			{ label: 'Gestione', func: function () { showcase.admin._render_sc (); } }
		    ];
	showcase.admin.register ( _acts );
	showcase.admin._am_showcase_init ( 'sc-workspace' );	
	showcase.admin._am_conf_init ( 'sc-conf-list' );


} ;

showcase.admin.init = function ( dest_div )
{
	//if ( ! dest_div ) showcase.admin._set_dest_div ( showcase.admin._dest_div );
	if ( dest_div ) showcase.admin._set_dest_div ( dest_div );

	// no longer supported showcase.admin._get_tags_list ();

	if ( ! showcase.templates ) 
	{
		showcase.load_templates ( showcase.admin._init_admin );
		return;
	} 
	showcase.admin._init_admin ();
};

/* BUNDLE */
showcase.admin._render_panel = function ( panel, title,  cback )
{
        var dct = { '_panel' : showcase.templates [ panel ], '_title' : title };
        $( showcase.admin._dest_div  , String.formatDict ( showcase.templates [ 'SC-ADMIN-LAYOUT' ], dct ) );
        if ( cback ) cback();
};

showcase.admin._render_sc = function ( dest_div )
{
        showcase.admin._render_panel ( 'SC-ADMIN-SHOWCASE', 'Gestione Showcase', function ()
        {
		showcase.admin.am_conf.show_list( dest_div);
		showcase.admin.add_conf();
        });
};

//{{{ Gestione Configurazioni

//{{{ ADMIN MANAGER CONF
showcase.admin._am_conf_init = function ( dest_div )
{
        if ( showcase.admin.am_conf ) return;
        showcase.admin.am_conf = new AdminManager
                                ( {
                                        'dest_res'    : dest_div,
                                        'name'          : 'am_conf',
                                        'list_action' : 'showcase.ajax.list_conf',
                                        'edit_action' : 'showcase.admin.edit_conf',
                                        'del_action' : 'showcase.admin.del_conf',
                                        'mod_menu_actions': [
                                                                 { func: "showcase.admin.add_conf()", 'class' : 'conf_new' , label: 'Nuova Configurazione' }
                                                        ]
                                } );
        
        
        showcase.admin.am_conf.set_row_fields (
                                [
                                        [ "Nome", "name" ],
					[ "L", "w" ],
					[ "H", "h"],
					[ "Playtime", "playtime" ]
                                ]
                                );

        showcase.admin.am_conf.set_row_actions ( [
                                                        [ showcase.templates [ 'SC-ADMIN-LIST-ACTION' ], "list_showcase" ]
                                                ] );
        showcase.admin.am_conf.cbacks [ 'row_manip' ] = showcase.admin._rwman_conf;
        showcase.admin.am_conf.cbacks [ 'edit_item' ] = function ( id ) { showcase.admin.edit_conf ( id ); };
        showcase.admin.am_conf.cbacks [ 'del_item' ] = function ( id ) { showcase.admin.del_conf ( id ); };
        showcase.admin.am_conf.cbacks [ 'list_showcase' ] = function ( id ) { showcase.admin.list_showcase ( id ); };
        showcase.admin.am_conf.cbacks [ 'pre_del' ] = showcase.admin._cat_pre_del;

        showcase.admin.am_conf.templates [ 'module_single_action' ] = showcase.templates [ 'module_single_action' ];
        showcase.admin.am_conf.templates [ 'actions_row_action' ] = showcase.templates [ 'sc_conf_actions_row_action' ];
};

showcase.admin._rwman_conf = function ( ds, row )
{
        row [ '_id' ] = row [ 'id' ];
};

//}}}

//{{{ Showcase.admin._confgi_form
showcase.admin._config_form = function ()
{
        var f = new liwe.form.instance ( 'config_form' );
        f.hidden ( 'id' , 0 );
        f.text ( { 'name' : 'name', 'size' : 20, 'maxlength' : 50, 'mandatory' : true, 'label' : 'Nome' } );
	f.number ( { 'name' : 'playtime', 'size' : 5, 'maxlength' : 50, 'mandatory' : true, 'label' : 'Playtime ( sec )' } );
	if ( showcase.admin._cur_id_conf == 0 )
	{
        	f.number ( { 'name' : 'w', 'size' : 5, 'maxlength' : 50, 'mandatory' : true, 'label' : 'Larghezza ( W )' } );
	        f.number ( { 'name' : 'h', 'size' : 5, 'maxlength' : 50, 'mandatory' : true, 'label' : 'Altezza ( H )' } );
	};
        f.button ( { value: 'Salva', onclick : "showcase.admin._save_conf()", 'class' : 'sc-save-btn' } );
        f.set ( 'sc-workspace' );
};
//}}}

//{{{ Configurazioni ADD EDIT SAVE DELETE
showcase.admin.add_conf = function ( dest )
{
	showcase.admin._cur_id_conf = 0;
	showcase.admin._cur_conf_name = '';
        $ ( 'title-work', 'Nuova Configurazione' );
        showcase.admin._config_form();
};

showcase.admin.edit_conf = function ( id_config )
{
        $ ( 'title-work', 'Modifica Configurazione' ); 
        showcase.admin._cur_id_conf = id_config ;
        showcase.admin._config_form();
        var v = { action : 'showcase.ajax.get_conf', id : id_config };
        showcase.ajax ( v , function ( data )
        {
                showcase.admin._cur_conf_name = data [ 'config' ] [ 'name' ]
                var f = liwe.form.get ( 'config_form' );
                data [ 'config' ].iterate ( function ( val, k )
                {
                        f.set_value ( k, val );
                });
                
        });
};

showcase.admin._save_conf = function ()
{
        var cback = showcase.admin._render_sc;
        var msg = 'Configurazione Salvata';
        var id_conf = showcase.admin._cur_id_conf;
        showcase.admin._cur_id_conf = 0;


        var f = liwe.form.get ( 'config_form' );
        if ( ! f.check () )
        {
		if ( typeof ( system.admin.do_alert ) == 'undefined') { alert ( 'Inserire tutti i valori' ); return }
                system.admin.do_alert ( 'Inserire tutti i valori', liwe.utils.notifier.mode.WARN );
                return;
        }

        var dct = f.get_values ();

        dct [ 'action' ] = "showcase.admin.add_conf";

	showcase.admin.ajax ( dct, function ( v ) 
	{
		if ( typeof ( system.admin.do_alert ) == 'undefined') { alert ( msg ); return }
                system.admin.do_alert ( msg, liwe.utils.notifier.mode.NOTICE );
                cback();
        } );

};

showcase.admin.del_conf = function ( ids )
{
	var conf = false;
	conf = confirm ( "Vuoi eliminare l'elemento ?" );
	if ( conf )
	{
		showcase.admin.ajax ( { action : "showcase.admin.del_conf", ids : ids }, function ( data )
					{
						showcase.admin._render_sc();
					} );
	}
};

//}}}

showcase.admin.list_showcase = function ( id_conf )
{
        var v = { action : 'showcase.ajax.get_conf', id : id_conf };
        showcase.ajax ( v , function ( data )
        {
                showcase.admin._cur_conf_name = data [ 'config' ] [ 'name' ];
                $( 'title-work' , 'Lista Immagini per - [ ' + showcase.admin._cur_conf_name + ' ]' );
                showcase.admin._cur_id_conf = id_conf ;
                showcase.admin.am_showcase.show_list ( 'sc-workspace' );
        });

	
};

//}}}

//{{{ Gestione Immagini collegate alla configurazione

//{{{ Admin Manager Immagini
showcase.admin._am_showcase_init = function ( dest_div )
{
        if ( showcase.admin.am_showcase ) return;
        showcase.admin.am_showcase = new AdminManager
                                ( {
                                        'dest_res': dest_div,
					'name' : 'am_showcase',
                                        'list_action': "showcase.admin.list",
                                        'del_action':  "showcase.admin.delete",
                                        'add_action':  "showcase.admin.add_item",
                                        'edit_action':  "showcase.admin.edit",
                                        'mod_menu_actions': [
                                                                 { func: "showcase.admin.add_item()", 'class' : 'conf_new' , label: 'Aggiungi Immagine' }
],
					'filter_form' : showcase.admin._am_showcase_filter_form,
					'filter_form_name' : "SC-filter-by"
                                } );

        showcase.admin.am_showcase.set_row_fields (
                                [
                                        [ "id", "id" ],
					[ "File name", "file_name" ],
					[ "Ext.", "ext" ],
					[ "Tipo", "kind" ],
					[ "Thumb", "_thumb" ],
					[ "created", "created" ]
                                ]
                                );

        showcase.admin.am_showcase.cbacks [ 'row_manip' ] = showcase.admin._rwman_showcase;
        showcase.admin.am_showcase.cbacks [ 'del_item' ]  = function ( id ) { showcase.admin.del ( id ); };
        showcase.admin.am_showcase.cbacks [ 'edit_item' ]  = function ( id ) { showcase.admin.edit ( id ); };
	showcase.admin.am_showcase.cbacks [ 'noresult' ] = function () { showcase.admin.add_item (); };
        showcase.admin.am_showcase.templates [ 'module_single_action' ] = showcase.templates [ 'module_single_action' ];

};

showcase.admin._rwman_showcase = function ( ds, row )
{
	row [ "_id" ] = row [ 'id' ];
	row [ "_thumb" ] = String.formatDict ( showcase.templates [ 'SC-image-thumb' ], { id : row [ 'id' ], width: 50 } );
};

showcase.admin._am_showcase_filter_form = function ( dest_div )
{       
        var f = new liwe.form.instance ( "SC-filter-by" );
        f.hidden ( "id_conf" , showcase.admin._cur_id_conf );
        f.set ( dest_div );
};


//}}}


//{{{ SHOCASE ADD EDIT SAVE FROM DELETE
showcase.admin.add_item = function ( id )
{
        $ ( 'title-work', 'Inserisci Nuova immgine a showcase [ ' + showcase.admin._cur_conf_name + ' ]' );
	showcase.admin._create_add_form ( 0 ) ;
};


showcase.admin._create_add_form = function ( id )
{
	var f = new liwe.form.instance ( 'SC-add-form' ); 

	f.events [ 'complete' ] = showcase.admin._refresh;
	f.hidden ( "action", 'showcase.admin.add' );
	f.hidden ( "__PYHP_SESSION", liwe.cookie.get ( "session" ) );
	f.hidden ( "id", id );
	f.hidden ( "id_conf", showcase.admin._cur_id_conf );
	if ( id == 0 )	f.upload ( { label : 'Immagine', name: 'upload', mandatory : true } );
	f.textarea ( { name: 'descr', value: '' , label: 'Descrizione', rows : 10 , cols : 39 } );
	f.text ( { name: 'lnk', value: '' , label: 'Link',  size : 34 } );
	f.submit ( { value: 'Salva', 'class' : 'sc-save-btn', nonl : true } );
        f.button ( { value: 'Annulla', onclick : "showcase.admin._refresh()", 'class' : 'sc-exit-btn' } );
	f.sep ();
	f.workspace ( { name: "SC-image" } );
	f.set ( 'sc-workspace' );

};


showcase.admin._refresh = function ()
{
        showcase.admin.am_showcase.show_list ( 'sc-workspace' );
};


showcase.admin.edit = function ( id )
{
	{
		showcase.admin.ajax ( { action : "showcase.admin.edit", id : id }, function ( data )
					{
						showcase.admin._create_add_form ( id );
						var f = liwe.form.get ( "SC-add-form" );
						['id', 'descr', 'lnk' ].iterate ( function ( v, k ) {
			                                n = data [ v ];
			                                if ( n === undefined ) n = 0;
                			                f.set_value ( v, n );
						} );

						data [ 'width' ] = 200;
						$( "SC-image" ).innerHTML = String.formatDict ( showcase.templates [ 'SC-image-thumb' ], data );
						return;
					} );
	}
};


showcase.admin.del = function ( id )
{
	var conf = false;
	conf = confirm ( "Vuoi eliminare l'elemento ?" );
	if ( conf )
	{
		showcase.admin.ajax ( { action : "showcase.admin.delete", ids : id }, function ( data )
					{
        					showcase.admin.am_showcase.show_list ( 'sc-workspace' );
					} );
	}
};

//}}}

//}}}



/* BUNDLE */
